package com.example.javabasic.sample.java8;

import java.util.HashMap;
import java.util.function.Supplier;

/**
 * @author: cph
 * @date: 2022/1/26 16:41
 */
public class EventMap<K, V> extends HashMap<K, V> {

    @Override
    public V get(Object key) {
        V v = super.get(key);
        if (v instanceof Supplier) {
            return ((Supplier<V>) v).get();
        } else {
            return v;
        }
    }

    public static void main(String[] args) {
        int a = 1;
        String s = "test";
        Supplier<Integer> supplier_a = () -> a + 1;
        Supplier<String> supplier_s = () -> s + "test";

        EventMap<String, Object> eventMap = new EventMap<>();
        eventMap.put("supplier_a", supplier_a);
        eventMap.put("supplier_s", supplier_s);
        eventMap.put("normal", "normal");

        System.out.println(eventMap.get("supplier_a"));
        System.out.println(eventMap.get("supplier_s"));
        System.out.println(eventMap.get("normal"));
    }
}
